1. Field of the Invention
The present invention relates to compiling machine language from source code. More particularly, this invention relates to methods for identifying and, in some embodiments, eliminating partially redundant memory references utilizing a static single assignment language during compiler optimization.
2. Description of Related Art
Most computer programmers write computer programs in source code using high-level languages such as BASIC, C, FORTRAN, or PASCAL. While programmers may easily understand such languages, modern computers are not able to directly read such languages. Thus, such computer programs must be translated into a language, known as machine language, that a computer can read and execute. One part in the translating process is performed by a compiler. A compiler translates a source code program, sometimes also called the source code, into object code. Object code is a machine language description of a high-level computer program.
The fundamentals of compiling a high-level language into object code are well known in the art. See, e.g., Aho et al., Compilers--Principles, Techniques and Tools (Addison-Wesley Publishing Co. 1988). These fundamentals include (1) generating an intermediate language representation of the source code, (2) determining the flow of control through the program, (3) determining the dominance relationships among the instructions constituting the program, and (4) partial redundancy elimination.
Object code produced by conventional compiling algorithms may often be "optimized," i.e., made to execute faster. Compilers that apply code-improving transformations are called optimizing compilers. Some conventional optimizing compilers translate high-level computer programs into an intermediate language known as a Static Single Assignment (SSA) representation before generating the object code. This SSA intermediate language is used as a basis to perform certain optimizations. After these optimizations are performed, these conventional optimizing compilers translate, or generate, the SSA intermediate language into optimized object code. A deeper explanation of SSA intermediate languages follows and employs the terminology set forth immediately below.
A statement in a computer program is said to "define" a variable if it assigns, or may assign, a value to that variable. For example, the statement "x=y+z" is said to "define" x. A statement that defines a variable contains a "definition" of that variable. In this context, there are two types of variable definitions: unambiguous definitions and ambiguous definitions. Ambiguous definitions may also be called complex definitions.
When a definition always defines the same variable, the definition is said to be an "unambiguous definition" of that variable. For example, the statement, "x=y" always assigns the value of y to x. Such a statement always defines the variable x with the value of y. Thus, the statement "x=y" is an "unambiguous definition" of x. If all definitions of a variable of code are unambiguous definitions, then the variable is known as an unambiguous variable. Some definitions do not always define the same variable. These definitions may possibly define different variables at different times in a computer program. Thus, they are called "ambiguous definitions." There are many types of ambiguous definitions and the principle common denominator among the many types is that they are not unambiguous definitions. One type of "ambiguous definition" occurs where a pointer refers to a variable. For example, the statement "*p=y" is a definition of x since it is possible that the pointer p points to x. Thus, the definition may ambiguously define any variable x if it is possible that p points to x. In other words, *p may define one of several variables depending on the value of p. Another type of ambiguous definition is a call of a procedure with a variable passed by reference. When a variable is passed by reference, the address of the variable is passed to the procedure. Passing a variable by reference to a procedure allows the procedure to modify the variable. Still another type of ambiguous definition is a procedure that may access a variable because that variable is within the scope of the procedure. Another type of ambiguous definition occurs when a variable is not within the scope of a procedure but the variable has been identified with another variable that is passed as a parameter or is within the scope of the procedure.
When a statement in a computer program references a variable, the statement is said to "use" the variable. For example, the statement "x=y+z" refers to and is said to "use" y and z while unambiguously defining x. Similarly, y and z (but not x) are "used" in the statement "x[y]=z" while unambiguously defining x[y]. A statement that uses a variable contains a "use" of that variable.
A definition of a variable "reaches" a use of that variable if that definition is the last definition of that variable prior to the use. Consider the following straight-line C pseudo code: EQU x=6 EQU x=x+5 EQU x=7 EQU x=x+8
The definition in the first statement "x=6" reaches the use in the second statement "x=x+5." Similarly, the definition in the third statement "x=7" reaches the use in the fourth statement "x=x+8." Note that the definition in the first statement does not reach the use of the fourth statement because x is redefined in the second and third statements.
In the above example, the unambiguous definition of x in the second statement is said to "kill" the definition of x in the first statement because it nullifies the effects of the definition in the first statement. Similarly, the definitions of x in the third and fourth statements kill the definitions in the second and third statements, respectively. The period of time between the definition and the definition's kill is known as the definition's "lifetime." Only unambiguous definitions of a variable can kill other definitions of the variable. Thus, a use can be reached by both an unambiguous definition and a subsequent ambiguous definition of the same variable.
A computer programmer may address a variable by specifying the variable's location in memory. This location is known as the variable's absolute address. This method of addressing is known as direct addressing. Direct addressing commonly occurs when a variable is specified by its name. For example, in the statement "y=x," both y and x are directly addressed.
A computer programmer may also address a variable by specifying an address that refers to a different address, which may specify yet another address. This method of addressing is known as indirect addressing. Common examples of indirect addressing include pointers, arrays and combinations of pointers and arrays. Examples of indirect addressing include a[i], *p, *(p+4), **p, a[b[i]], and *(*p+4). When a variable is indirectly addressed, at least one indirect memory reference is employed to determine the absolute address of the variable.
A variable may be classified based upon the number of indirect memory references employed to determine the absolute address of the variable. For example, as discussed above, y and x may be directly addressed. Thus, there are zero indirect memory references employed to determine the absolute address of both y and x. These variables are known as rank-0 variables.
A variable employing a single indirect memory reference is known as a rank-1 variable. Examples of rank-1 variables include single pointer references and single array references such as a[i], *p, and *(p+4). A variable that requires two indirect memory references is known as a rank-2 variable. Rank-2 variables include double pointer references and double array references and the combination of a single pointer reference and a single array reference. Examples of rank-2 variables include **p, a[b[i]], and *(*p+4). A rank-n variable employs n indirect memory references to determine the absolute address of the variable.
A definition that defines a rank-n variable is known as a rank-n definition. Similarly a use of a rank-n variable is known as a rank-n use. For example, the definition of the array element b[a[i]] is a rank-0 use of the variable i, a rank-1 use of the array element a[i], and a rank-2 definition of the array element b[a[i]].
Returning to the discussion of SSA intermediate languages, when a computer program is conventionally translated into a SSA intermediate language, each variable definition is given a unique name. Further, all the uses reached by that definition are also renamed to match the variable's new name. For example, consider the straight-line C pseudo code discussed above. When this C pseudo code is translated into a SSA intermediate language, the result would be the following: EQU t.sub.1 =6 EQU t.sub.2 =t.sub.1 +5 EQU t.sub.3 =7 EQU t.sub.4 =t.sub.3 +8
The symbols t.sub.1 through t.sub.4 are known as compiler temporaries or even more commonly as temps. Unlike most variables, temps have only a single definition. Because a temp has only a single definition, it may not be ambiguously defined and are unaliasable scalars. Because temps are unaliasable scalars, an expression using t.sub.1 has a different symbolic meaning from the symbolic meaning of an otherwise identical expression using i. Every use of i cannot be considered equal because i represents an aliasable variable. However, every use of t.sub.1 can be considered equal. While a compiler may not be able to determine the value contained in a temp, every use of that temp will return the same unknown value. Therefore, temps dramatically simplify certain compiler algorithms.
Unlike the above straight-line C pseudo code, programs typically also contain branch statements. A branch statement is a statement that selects one set of statements from a number of alternative sets of statements. For example, consider the following if-then-else statement: EQU if (p) then EQU {x=4} EQU else EQU {x=6} EQU x=2+x
The flow of control through this segment of code during execution will branch depending on whether p is true or false and will unite again at the statement "x=2+x." The point where it branches is known as the "branch point" and the point where it unites is known as the "join point" or "confluence point."
When this C pseudo code is translated into a SSA intermediate language, the result would be the following: EQU if (p) then EQU {t.sub.1 =4} EQU else EQU {t.sub.2 =6} EQU t.sub.3 =.PHI.(t.sub.1,t.sub.2) EQU t.sub.4 =2+t.sub.3
Depending on the value of p, either t.sub.1 will be defined as 4 or t.sub.2 will be defined as 6. In order to "join" these two definitions, a special definition called a phi-function is inserted at the point where the branches join. Phi-functions are known by those skilled in the art.
The above phi-function contains two operands. An operand is a quantity that enters into (or results from) an operation. The operands indicate which definitions reach the join point. In this example, both t.sub.1 and t.sub.2 reach the join point. Thus, both t.sub.1 and t.sub.2 are operands to the phi-function that defines t.sub.3. As shown above, subsequent uses of x in the original program would use t.sub.3 in the corresponding SSA intermediate language.
Conventional SSA intermediate languages can accommodate only rank-0 variables. Ambiguous definitions and uses reached by ambiguous definitions cannot be renamed as temps. Phi-nodes also cannot be inserted in conventional SSA intermediate languages without temps. Therefore, phi-nodes cannot conventionally be inserted in the presence of ambiguity interjected by ambiguous definitions and their uses. Thus, rank-1 and rank-2 variables are not included in conventional SSA intermediate languages. Because such intermediate languages contain only a limited amount of symbolic information, only limited optimizations may be based on such languages. Thus, in order to perform significant optimizations, numerous ad hoc algorithms are employed. These conventional algorithms are inefficient, incomplete, not well defined, and complex.
Partial redundancy elimination ("PRE") is one type of optimization to which flow control and dominance are important concepts. Removing memory references is the most important optimization possible during compilation. One type of unnecessary memory reference that may be removed is known as a "partially redundant" memory reference. A partially redundant memory reference is a memory reference that is identical to a prior memory reference occurring along one control flow path but not all control flow paths. To reduce execution time, the partially redundant memory references should be eliminated.
A number of PRE techniques are known to the art. See, e.g., Agrawal, et al., "Interprocedural Partial Redundancy Elimination and Its Application to Distributed Memory Compilation." However, a weakness of all conventional PRE methods is efficiently and accurately determining the optimal position to move code so that uses are dominated by one definition and the resulting lifetime is minimal. Conventional PRE methods are employed, if at all, as part of the cumbersome, inefficient, ad hoc optimization performed on rank-0 intermediate language representations just prior to generating machine readable object code. These conventional methods also necessarily involve numerous bitsets and complex iterative data flow calculations to identify partial redundancies. Still further, PRE typically is not performed on even rank-0 SSA intermediate language representations for these and other reasons. Thus, a need exists for a quicker, more efficient method to eliminate partially redundant memory references in a wider variety of contexts.